package vin.echo.core.configure;

import java.io.File;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

public class WebXmlConfiguration implements EchoConfiguration {

	private Logger logger = Logger.getLogger(WebXmlConfiguration.class);
	private Map<String, File> serviceRootPath;

	public WebXmlConfiguration(ServletContext context) {
		serviceRootPath = new HashMap<String, File>();
		handleWebContext(context);
	}

	private void handleWebContext(ServletContext context) {
		String packages = context
				.getInitParameter(ConfigConstants.PACKAGE_ROOT);
		String packageDirName = "/" + packages.replace('.', '/') + "/";
		logger.info(packages);
		try {
			Enumeration<URL> dirs = Thread.currentThread()
					.getContextClassLoader().getResources(packageDirName);

			while (dirs.hasMoreElements()) {
				URL url = dirs.nextElement();
				File file = new File(url.toURI());
				if (file.exists() && file.isDirectory()) {
					serviceRootPath.put(packages, file);
				}
			}
		} catch (Exception e) {
			logger.error("error path");
		}
	}

	@Override
	public Map<String, File> getServiceRootPath() {
		return serviceRootPath;
	}

}
